﻿Imports TBCC.UtilityMobile
Public Class CarHDService

    Public Shared Function GetHDCarList(ByVal parentId As String, ByVal projectId As String, ByVal startTime As String, ByVal endTime As String, ByVal timeValue As String) As IList(Of ProjectInfo.CarHDItem)
        Dim carHDList As IList(Of ProjectInfo.CarHDItem)
        carHDList = New List(Of ProjectInfo.CarHDItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and UpdateTime between '{1}' and '{2}' and ((HOUR(updatetime)*3600+ MINUTE(updatetime)*60+SECOND(updatetime))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)
        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and UpdateTime between '{1}' and '{2}' and ((HOUR('{2}')*3600+ MINUTE('{2}')*60+SECOND('{2}'))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)


        strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} and ((HOUR(updatetime)*3600+ MINUTE(updatetime)*60+SECOND(updatetime))- (HOUR('{1}')*3600+ MINUTE('{1}')*60+SECOND('{1}'))) % {4} =0", projectId, startTime, endTime, parentId, timeValue)

        'strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={3} ", projectId, startTime, endTime, parentId, timeValue)
        dt = SqlHelper.ExecuteDataset(strsql, Nothing)

        For Each r As DataRow In dt.Tables(0).Rows
            Dim car As New ProjectInfo.CarHDItem


            car.histDataStorageType = SqlHelper.GetDbData(r("histDataStorageType"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            'If car.histDataStorageType = 0 AndAlso ((car.UpdateTime.Hour * 3600 + car.UpdateTime.Minute * 60 + car.UpdateTime.Second) Mod timeValue <> 0) Then
            '    Continue For

            'End If

            '  car.TransportState = SqlHelper.GetDbData(r("TransportState"))

            car.ParentId = SqlHelper.GetDbData(r("ParentId"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            car.Latitude_dir = SqlHelper.GetDbData(r("Latitude_dir"))
            car.Latitude = SqlHelper.GetDbData(r("Latitude"))
            Dim count As Integer = car.Latitude.ToString.Length

            Dim str As String
            Dim str1 As String
            If car.Latitude_dir = 1 Then
                str = "'N"
            ElseIf car.Latitude_dir = 0 Then
                str = "'S"
            Else
                str = ""
            End If
            If count > 0 Then
                'msg.Show("car.Latitude.IndexOf('.')=" + car.Latitude.IndexOf(".").ToString)
                Dim start As String = car.Latitude.Substring(0, car.Latitude.IndexOf(".")) '找到这一点

                If start.Equals("-300") Then
                    car.Latitude = "---"
                    'Exit For

                Else

                    Dim first As String = ""
                    If start.Length > 0 Then

                        If start.Length >= 2 Then
                            If start.Length = 2 Then
                                first = start.Substring(0, start.Length - 2) + "00°"
                            Else
                                first = start.Substring(0, start.Length - 2) + "°"

                            End If
                            Dim secoud As String = start.Substring(start.Length - 2)
                            'msg.Show("car.Latitude.IndexOf('.')=" + car.Latitude.IndexOf(".").ToString)
                            'msg.Show("count=" + count.ToString)
                            Dim endString As String = car.Latitude.Substring(car.Latitude.IndexOf("."))
                            car.Latitude = first + secoud + endString + str
                        Else
                            car.Latitude = start + "°00.00" + str
                        End If



                    Else
                        car.Latitude = "---"

                    End If
                End If


            Else
                car.Latitude = "---"

            End If


            car.Longitude_dir = SqlHelper.GetDbData(r("Longitude_dir"))
            car.Longitude = SqlHelper.GetDbData(r("Longitude"))

            Dim countL As Integer = car.Longitude.ToString.Length
            If car.Longitude_dir = 0 Then
                str1 = "'E"
            ElseIf car.Longitude_dir = 1 Then
                str1 = "'W"
            Else
                str1 = ""
            End If

            If countL > 0 Then
                Dim start As String = car.Longitude.Substring(0, car.Longitude.IndexOf(".")) '找到这一点
                If start.Equals("-300") Then
                    car.Longitude = "---"

                Else

                    Dim first As String = ""
                    If start.Length > 0 Then

                        If start.Length >= 2 Then
                            If start.Length = 2 Then
                                first = start.Substring(0, start.Length - 2) + "00°"
                            Else
                                first = start.Substring(0, start.Length - 2) + "°"

                            End If
                            Dim secoud As String = start.Substring(start.Length - 2)

                            Dim endString As String = car.Longitude.Substring(car.Longitude.IndexOf("."))
                            car.Longitude = first + secoud + endString + str1
                        Else
                            car.Longitude = start + "°00.00" + str1
                        End If



                    Else
                        car.Longitude = "---"

                    End If

                End If
            Else
                car.Longitude = "---"

            End If

            car.AlarmStatus = SqlHelper.GetDbData(r("AlarmStatus"))

            car.AI1 = SqlHelper.GetDbData(r("AI1"))
            car.AI2 = SqlHelper.GetDbData(r("AI2"))
            car.AI3 = SqlHelper.GetDbData(r("AI3"))
            car.AI4 = SqlHelper.GetDbData(r("AI4"))
            If car.AI1 <> "-300.00" AndAlso car.AI2 <> "-300.00" AndAlso car.AI3 <> "-300.00" Then
                '设置最大温度
                If car.AI1 > car.AI2 AndAlso car.AI1 > car.AI3 Then
                    car.MaxTemper = car.AI1
                ElseIf car.AI2 > car.AI1 AndAlso car.AI2 > car.AI3 Then
                    car.MaxTemper = car.AI2
                Else
                    car.MaxTemper = car.AI3
                End If
                '设置最小温度
                If car.AI1 < car.AI2 AndAlso car.AI1 < car.AI3 Then
                    car.MinTemper = car.AI1
                ElseIf car.AI2 < car.AI1 AndAlso car.AI2 < car.AI3 Then
                    car.MinTemper = car.AI2
                Else
                    car.MinTemper = car.AI3

                End If
                '平均温度
                car.AvgTemper = System.Math.Round(((Convert.ToDecimal(car.AI1) + Convert.ToDecimal(car.AI2) + Convert.ToDecimal(car.AI3)) / 3), 2, MidpointRounding.AwayFromZero)
            ElseIf car.AI1 = "-300.00" AndAlso car.AI2 <> "-300.00" AndAlso car.AI3 <> "-300.00" Then
                If car.AI2 > car.AI3 Then
                    car.MaxTemper = car.AI2
                    car.MinTemper = car.AI3
                Else
                    car.MaxTemper = car.AI3
                    car.MinTemper = car.AI2
                End If

                '平均温度
                car.AvgTemper = System.Math.Round(((Convert.ToDecimal(car.AI2) + Convert.ToDecimal(car.AI3)) / 2), 2, MidpointRounding.AwayFromZero)
            ElseIf car.AI2 = "-300.00" AndAlso car.AI1 <> "-300.00" AndAlso car.AI3 <> "-300.00" Then
                If car.AI1 > car.AI3 Then
                    car.MaxTemper = car.AI1
                    car.MinTemper = car.AI3
                Else
                    car.MaxTemper = car.AI3
                    car.MinTemper = car.AI1
                End If

                '平均温度
                car.AvgTemper = System.Math.Round(((Convert.ToDecimal(car.AI1) + Convert.ToDecimal(car.AI3)) / 2), 2, MidpointRounding.AwayFromZero)
            ElseIf car.AI3 = "-300.00" AndAlso car.AI1 <> "-300.00" AndAlso car.AI2 <> "-300.00" Then
                If car.AI1 > car.AI2 Then
                    car.MaxTemper = car.AI1
                    car.MinTemper = car.AI2
                Else
                    car.MaxTemper = car.AI2
                    car.MinTemper = car.AI1
                End If

                '平均温度
                car.AvgTemper = System.Math.Round(((Convert.ToDecimal(car.AI1) + Convert.ToDecimal(car.AI2)) / 2), 2, MidpointRounding.AwayFromZero)
            ElseIf car.AI1 = "-300.00" AndAlso car.AI2 = "-300.00" AndAlso car.AI3 = "-300.00" Then
                car.MaxTemper = "---"
                car.MinTemper = "---"
                car.AvgTemper = "---"
            ElseIf car.AI1 = "-300.00" AndAlso car.AI2 = "-300.00" AndAlso car.AI3 <> "-300.00" Then
                car.MaxTemper = car.AI3
                car.MinTemper = car.AI3
                car.AvgTemper = car.AI3
            ElseIf car.AI1 = "-300.00" AndAlso car.AI2 <> "-300.00" AndAlso car.AI3 = "-300.00" Then
                car.MaxTemper = car.AI2
                car.MinTemper = car.AI2
                car.AvgTemper = car.AI2
            ElseIf car.AI1 <> "-300.00" AndAlso car.AI2 = "-300.00" AndAlso car.AI3 = "-300.00" Then
                car.MaxTemper = car.AI1
                car.MinTemper = car.AI1
                car.AvgTemper = car.AI1




            End If

            car.TransportState = SqlHelper.GetDbData(r("TransportState"))
            '  car.AlarmStatus = SqlHelper.GetDbData(r("histAlarmData"))



            carHDList.Add(car)




        Next
        Return carHDList
    End Function
    Public Shared Function GetFDAPHDCarList(ByVal parentId As String, ByVal projectId As String) As IList(Of ProjectInfo.CarHDItem)

        Dim carHDList As IList(Of ProjectInfo.CarHDItem)
        carHDList = New List(Of ProjectInfo.CarHDItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql += String.Format("select * from tbcchistdata_car_{0}_1 where parentId={1} and fdapfinished={2} limit 10", projectId, parentId, 0)
        dt = SqlHelper.ExecuteDataset(strsql, Nothing)

        For Each r As DataRow In dt.Tables(0).Rows

            Dim car As New ProjectInfo.CarHDItem


            car.ParentId = SqlHelper.GetDbData(r("ParentId"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            car.Latitude_dir = SqlHelper.GetDbData(r("Latitude_dir"))
            car.Latitude = SqlHelper.GetDbData(r("Latitude"))
            car.Longitude_dir = SqlHelper.GetDbData(r("Longitude_dir"))
            car.Longitude = SqlHelper.GetDbData(r("Longitude"))

            car.AlarmStatus = SqlHelper.GetDbData(r("AlarmStatus"))

            car.AI1 = SqlHelper.GetDbData(r("AI1"))
            car.AI2 = SqlHelper.GetDbData(r("AI2"))
            car.AI3 = SqlHelper.GetDbData(r("AI3"))
            car.AI4 = SqlHelper.GetDbData(r("AI4"))

            carHDList.Add(car)

        Next



        Return carHDList
    End Function
    Public Shared Function GetFDAPCarList(ByVal projectId As String) As IList(Of ProjectInfo.CarHDItem)

        Dim carHDList As IList(Of ProjectInfo.CarHDItem)
        carHDList = New List(Of ProjectInfo.CarHDItem)

        Dim dt As DataSet
        Dim strsql As String = ""
        strsql = String.Format("select * from tbcchistdata_car_{0}_1 where  fdapfinished={1} order by UpdateTime limit 10", projectId, 0)
        dt = SqlHelper.ExecuteDataset(strsql, Nothing)

        For Each r As DataRow In dt.Tables(0).Rows

            Dim car As New ProjectInfo.CarHDItem


            car.ParentId = SqlHelper.GetDbData(r("ParentId"))
            car.UpdateTime = SqlHelper.GetDbData(r("UpdateTime"))
            car.Latitude_dir = SqlHelper.GetDbData(r("Latitude_dir"))
            car.Latitude = SqlHelper.GetDbData(r("Latitude"))
            car.Longitude_dir = SqlHelper.GetDbData(r("Longitude_dir"))
            car.Longitude = SqlHelper.GetDbData(r("Longitude"))

            car.AlarmStatus = SqlHelper.GetDbData(r("AlarmStatus"))

            car.AI1 = SqlHelper.GetDbData(r("AI1"))
            car.AI2 = SqlHelper.GetDbData(r("AI2"))
            car.AI3 = SqlHelper.GetDbData(r("AI3"))
            car.AI4 = SqlHelper.GetDbData(r("AI4"))

            carHDList.Add(car)

        Next



        Return carHDList
    End Function
    ''' <summary>
    ''' 查找数据中最后一条
    ''' </summary>
    ''' <param name="carPrp"></param>
    ''' <param name="pid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetLastData(ByVal carPrp As String, ByVal pid As Integer) As DateTime
        Dim strSql As String = ""
        strSql += String.Format("select max(UpdateTime) from TbccHistData_Car_{0}_1 where ParentId={1} ", carPrp, pid)
        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return DateTime.MinValue
        End If

        Return obj

    End Function

    ''' <summary>
    ''' 上传对应启停记录数据
    ''' </summary>
    ''' <param name="pi"></param>
    ''' <param name="StartStopid"></param>
    ''' <param name="carHD"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>

    Public Shared Function SetHDData(ByVal pi As String, ByVal StartStopid As Integer, ByVal carHD() As CarWebServiceAdd.CarHisView_new) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("Insert Into TbccHistData_Car_{0}_1(ParentId,UpdateTime,AI1,AI2,AI3,AI4,Latitude_dir,Latitude,Longitude_dir,Longitude,AlarmStatus,fdapfinished,TransportState,histAlarmStorageType,gpsStorageType,histDataStorageType,unloadStorageType,histAlarmData) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12,?p13,?p14,?p15,?p16,?p17);-- ", pi)

        For k As Integer = 0 To carHD.Length - 1
            Dim CarHDItem As CarWebServiceAdd.CarHisView_new
            CarHDItem = carHD(k)

            If CarHDItem.updateTime = Nothing Then
                Exit Function
            End If
            Dim p(17) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p.GetUpperBound(0)
                p(i) = New MySql.Data.MySqlClient.MySqlParameter
                p(i).ParameterName = "p" + i.ToString
            Next
            p(0).Value = StartStopid
            p(1).Value = CarHDItem.updateTime
            p(2).Value = CarHDItem.ai1
            p(3).Value = CarHDItem.ai2
            p(4).Value = CarHDItem.ai3
            p(5).Value = CarHDItem.ai4
            p(6).Value = CarHDItem.latitude_dir
            p(7).Value = CarHDItem.latitude
            p(8).Value = CarHDItem.longitude_dir
            p(9).Value = CarHDItem.longitude
            p(10).Value = CarHDItem.alarmStatus
            p(11).Value = 0
            p(12).Value = CarHDItem.unloadStatus
            p(13).Value = CarHDItem.histAlarmStorageType
            p(14).Value = CarHDItem.gpsStorageType
            p(15).Value = CarHDItem.histDataStorageType
            p(16).Value = CarHDItem.unloadStorageType
            p(17).Value = CarHDItem.histAlarmData

            tic.Add(strSql & k, p)

        Next


        Return SqlHelper.ExecuteTranstration(tic)
    End Function

    Public Shared Function UpdateFDAPState(ByVal pi As String, ByVal StartStopid As Integer, ByVal update As DateTime) As Boolean

        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("update  TbccHistData_Car_{0}_1 set Fdapfinished=?p0 where ParentId=?p1 and UpdateTime=?p2", pi)

        Dim p(2) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = 1
        p(1).Value = StartStopid
        p(2).Value = update


        tic.Add(strSql, p)

        Return SqlHelper.ExecuteTranstration(tic)

    End Function
    Public Shared Function UpdateCARFDAPState(ByVal pi As String, ByVal update As DateTime) As Boolean

        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        strSql = String.Format("update  TbccHistData_Car_{0}_1 set Fdapfinished=?p0 where  UpdateTime=?p1", pi)

        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).Value = 1
        p(1).Value = update

        tic.Add(strSql, p)

        Return SqlHelper.ExecuteTranstration(tic)

    End Function
    '''' <summary>
    '''' 查找数据中最后一条
    '''' </summary>
    '''' <param name="carPrp"></param>
    '''' <param name="pid"></param>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Public Shared Function GetLastData(ByVal carPrp As String, ByVal pid As Integer) As DateTime
    '    Dim strSql As String = ""
    '    strSql += String.Format("select max(UpdateTime) from TbccHistData_Car_{0}_1 where ParentId={1} ", carPrp, pid)
    '    Dim obj As Object
    '    obj = SqlHelper.ExecuteScalar(strSql, Nothing)

    '    If obj Is Nothing OrElse IsDBNull(obj) Then
    '        Return DateTime.MinValue
    '    End If

    '    Return obj

    'End Function
End Class
